package net.asrsmid.invoker.as;

import java.util.List;

/**
 * public class CircleLinkedList<T> <br><br>
 * The class CircleLinkedList is data container in which data is organized in circle.
 * @author Chen WANG
 * @since 19 June 2011
 */
public class CircleLinkedList<T> {
	private List<T> data;
	private int counter;
	private int c;
	
	/**
	 * Constructs a CircleLinkedList with then the given fields. <br>
	 * Elements will not be repeated when calling 'rotate' method
	 * @param data a data list
	 */
	public CircleLinkedList(List<T> data) {
		this.data = data;
		this.counter = -1;
		this.c = -1;
	}
	
	/**
	 * Constructs a CircleLinkedList with then the given fields.
	 * @param data a data list
	 * @param elementRepeatNum repeat time for each element when calling 'rotate' method
	 */
	public CircleLinkedList(List<T> data, int elementRepeatNum) {
		this.data = data;
		this.counter = elementRepeatNum;
		this.c = elementRepeatNum;
	}
	
	/**
	 * Returns the element at the top position. If the repeat times for each
	 * element is reached, then the element at the top position will be moved
	 * to the last position and the next element of the top will be the element
	 * at the top position
	 * @return the element currently at the top position
	 */
	public T rotate() {
		if (counter == -1) {
			T d = data.remove(0);
			data.add(d);
			return d;
		} else {
			if (c > 1) {
				c--;
				return data.get(0);
			} else {
				T d = data.remove(0);
				data.add(d);
				c = counter;
				return d;
			}
		}
	}
}